#include <stdlib.h>
#include <stdio.h>

const char *iter = "iter";
const char *rec = "rec";

void print_usage_and_exit()
{
	printf("Usage: factorial <n> <%s|%s> ... to produce n!", iter, rec);
	printf("\n");
	printf("    where...\n");
	printf("        - n = the input to the resulting factorial, n!\n");
	printf("        - %s = iterative solution\n", iter);
	printf("	- %s = recursive solution\n", rec);
	exit(-1);
}

int main(int argc, char *argv[])
{
	int n, n_fact;
	char *type;

	if (argc != 3)
		print_usage_and_exit();

	n_fact = -1;
	n = atoi(argv[1]);
	type = argv[2];
	if (0 == strcmp(type, iter))
		n_fact = fact_iter(n);
	else if (0 == strcmp(type, rec))
		n_fact = fact_rec(n);
	else
		print_usage_and_exit();

	printf("n! = %d\n", n_fact);
	return 0;
}



int fact_iter(int n)
{
	int n_fact, k;

	n_fact = n;
	for (k=n-1; k>=1; k--)
		n_fact *= k;

	return n_fact;
}

int fact_rec(int n)
{
	return (n == 1) ? 1 : n * fact_rec(n-1);
}
